#include "Ark.h"
#include "StringOps.h"
#include "Neptuner.h"

//Crude singleton
static Ark gxArk;
using namespace std;

void Ark_addScuba(Scuba* pxScuba)
{
	gxArk.m_apxScuba[pxScuba->m_sFilename] = pxScuba;
}

Radar* Ark_getRadar()
{
	return &(gxArk.m_xRadar);
}

ScubaPack * Ark_getScubaPack()
{
	return &(gxArk.m_apxScuba);
}

std::string Ark_getAbsolutePath(std::string sFilename)
{
	bfs::path xTest;
	xTest = bfs::complete(bfs::path("./" + sFilename,bfs::native));
	xTest = bfs::system_complete(xTest);
	if(bfs::exists(xTest)) 
		return xTest.string(); //No need to search if in current directory

	return gxArk.m_xRadar.getAbsolutePath(sFilename);
}

Scuba* Ark_isParsed(std::string sFilename)
{
	Scuba* pxItem = NULL;

	pxItem=gxArk.m_apxScuba[sFilename];
	if(pxItem){
		return pxItem;
	}
	return NULL;
}

std::string Ark_getCodebaseLocation(std::string sFullPath)
{
	return gxArk.m_xRadar.getCodebaseLocation(sFullPath);
}

std::string Ark_getLinkForSymbol(Symbol xSymbol, Scope* pxScope, int nCurrentLevel)
{
	std::string sTokenHTML = xSymbol.m_sName;

	if(!pxScope->findSymbol(sTokenHTML, xSymbol))
	{
		return sTokenHTML;
	}

	sTokenHTML = "<a href='";
	std::string sTargetFile = Ark_getCodebaseLocation(xSymbol.m_xDefinition.m_sFilename);
	sTargetFile += ".uboat.html";
	std::string sRelativizer = Neptuner_getRelativizer(nCurrentLevel)+"codebase/";
	sTargetFile = sRelativizer + sTargetFile;
	sTokenHTML += sTargetFile;
	sTokenHTML += "#SL";
	sTokenHTML += stringify(xSymbol.m_xDefinition.m_nLineNumber);
	sTokenHTML += "'>";
	sTokenHTML += xSymbol.m_sName;
	sTokenHTML += "</a>";

	return sTokenHTML;
}